package com.naizhuai.Model.db.ainote;

import android.content.Context;
import android.support.annotation.NonNull;

import com.naizhuai.AppContext;
import com.naizhuai.Model.db.AppDatabase;
import com.naizhuai.base.AppExecutors;

import java.util.List;

import static com.naizhuai.util.AutoClearedValue.checkNotNull;

/**
 * @author：lichenxi
 * @date 2018/2/22 12
 * email：525603977@qq.com
 * Fighting
 */
public class LocalNoteData implements NoteDataSourse {
    private static volatile LocalNoteData INSTANCE;

    private @NonNull NoteDao mNoteDao;

    private @NonNull AppExecutors mAppExecutors;


    public static LocalNoteData getInstance() {
        if (INSTANCE == null) {
            synchronized (LocalNoteData.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LocalNoteData();
                }
            }
        }
        return INSTANCE;
    }
    public LocalNoteData() {
        mAppExecutors = new AppExecutors();
        mNoteDao = AppDatabase.getInstance().NoteDao();
    }

    @Override
    public void getNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                List<Note> notes=mNoteDao.getNotes();
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (notes.isEmpty()){
                            callback.onDataNotAvailable();
                        }else {
                            callback.onNotesLoaded(notes);
                        }
                    }
                });
            }
        });
    }

    @Override
    public void getNote(@NonNull final  int noteId, @NonNull GetNoteCallback callback) {
          checkNotNull(callback);
           mAppExecutors.diskIO().execute(new Runnable() {
               @Override
               public void run() {
                    Note note=mNoteDao.getNoteById(noteId);
                   mAppExecutors.mainThread().execute(new Runnable() {
                       @Override
                       public void run() {
                           if (note != null) {
                               callback.onNoteLoaded(note);
                           } else {
                               callback.onDataNotAvailable();
                           }
                       }
                   });
               }
           });
    }

    @Override
    public void saveNote(@NonNull final Note note) {
        checkNotNull(note);
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                mNoteDao.insertTask(note);
            }
        });
    }

    @Override
    public void updateNote(@NonNull Note note) {
        checkNotNull(note);
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
              mNoteDao.updateNote(note);
            }
        });
    }

    @Override
    public void getTitleNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                List<Note> notes=mNoteDao.getTitleNotes();
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (notes.isEmpty()){
                            callback.onDataNotAvailable();
                        }else {
                            callback.onNotesLoaded(notes);
                        }
                    }
                });
            }
        });
    }

    @Override
    public void getCreationNotes(@NonNull LoadNotesCallback callback) {
        checkNotNull(callback);
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                List<Note> notes=mNoteDao.getCreationNotes();
                mAppExecutors.mainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        if (notes.isEmpty()){
                            callback.onDataNotAvailable();
                        }else {
                            callback.onNotesLoaded(notes);
                        }
                    }
                });
            }
        });
    }


    @Override
    public void deleteNotes(List<Note> notes) {
        mAppExecutors.diskIO().execute(new Runnable() {
            @Override
            public void run() {
                mNoteDao.deleteNotes(notes);
            }
        });
    }
}
